<!--头部公共部分-->
{include file="common/header" /}
<div style="padding:15px;">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>上课时间</legend>
    </fieldset>
    <div class="site-demo-button" lay-filter="selBtn" id="typeclass">
        <button class="layui-btn layui-btn-sm site-demo-active layui-btn-warm" data-type="all">全部</button>
        <button class="layui-btn layui-btn-sm site-demo-active" data-type="gzr">工作日</button>
        <button class="layui-btn layui-btn-sm site-demo-active" data-type="zm">周末</button>
    </div>
    <table class="layui-table" id="datalist" lay-filter="Event"></table>
    <!--<script type="text/html" id="barTable">-->
        <!--<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>-->
    <!--</script>-->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" id="class_room" value="{$class_room}"/>
            <input type="hidden" id="class_id" value="{$id}"/>
            <a class="layui-btn layui-btn-primary" href="{:url('Grades/lists')}">返回</a>
        </div>
    </div>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.use(['table','jquery','element','form','layer'], function(){
        let $ = layui.jquery
            ,layer = layui.layer
            ,table = layui.table
            ,element = layui.element
            ,form = layui.form;

        let loading = layer.load();
        //方法级渲染
        let tableIns = table.render({
            elem: '#datalist'
            , url: '{:url("Schooltime/lists")}'
            , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {type: 'checkbox'},
                {field: 'week', title: '星期', align: 'center'},
                {field: 'start_time', title: '开始时间', align: 'center', edit: 'text'},
                {field: 'end_time', title: '结束时间', align: 'center', edit: 'text'},
                // {field: 'class_room', title: '教室', align: 'center', edit: 'text'},
                // {fixed: 'right', title: '操作', width: 100, align: 'center', toolbar: '#barTable'},
            ]]
            , page: false
            , done: function () {
                layer.close(loading);
            }
        });

        //订单分类选择
        $('.site-demo-active').on('click', function () {
            $('#typeclass').find('button').removeClass('layui-btn-warm');
            $(this).addClass('layui-btn-warm');
            let type = $(this).data('type');
            tableIns.reload({
                where: {
                    'type': type
                }
            });
        });

        table.on('edit(Event)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            if (field === 'end_time') {
                $.post('{:url("Schooltime/edit")}'
                    , {
                        'id': data.id,
                        'start_time': data.start_time,
                        'end_time': value
                    }
                    , function (result) {
                        layer.msg(result.msg);
                        if (result.code === 1) {
                            tableIns.reload();
                        }
                    });
            }else if (field === 'start_time') {
                $.post('{:url("Schooltime/edit")}'
                    , {
                        'id': data.id,
                        'end_time': data.end_time,
                        'start_time': value
                    }
                    , function (result) {
                        layer.msg(result.msg);
                        if (result.code === 1) {
                            tableIns.reload();
                        }
                    });
            }
        });

        //监听提交
        form.on('submit(form)', function (data) {
            var field = data.field;
            var isLoad;
            $.ajax({
                url: '{:url("Grades/add")}',
                method: "post",
                data: field,
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    }); //0代表加载的风格，支持0-2
                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 1) {
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            parent.location.reload();
                        }, 2000);
                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
            return false;
        });
        //监听头工具栏事件
        table.on('toolbar(Event)', function(obj){
            let checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    let status = checkStatus.isAll ? '全选': '未全选';
                    if(status === '未全选'){
                        layer.msg('未全选');
                    }else{
                        let checked_ids = [];
                        let file_id= 0;
                        for (let index = 0; index < data.length; index++) {
                            const element = data[index];
                            checked_ids.push(element.id)
                            file_id = element.file_id;
                        }
                        let ids ={"ids":checked_ids.join(',')};
                        let class_room = $("#class_room").val();
                        let class_id = $("#class_id").val();
                        let url = '{:url("Schooltime/binding")}?id='+ class_room + '&class_id='+class_id;
                        $.post(url,ids,function(response){
                            console.log(response);
                            if(response.code === 1){
                                tableIns.reload({
                                    where: {
                                        'class_id': response.data
                                    }
                                });
                            }else{
                                layer.msg(response.msg);
                            }
                        });
                    }

                    break;
                case 'update':
                    layer.msg('暂不启用该功能');
                    break;
                case 'delete':
                    layer.msg('暂不启用该功能');
                    break;
            };
        });
    });
</script>